package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl  implements GuestbookService {

    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao =new GuestbookDaoImpl();
        //DBManager中获取到的连接传递给BaseDao中的setConnection(conn)
        guestbookDao.setConnection(connection);
        boolean isRight = false;
        try {
            isRight =  guestbookDao.addMessage(guestbook);
            connection.commit();
        } catch (SQLException e){
            try {
                connection.rollback();
            } catch (SQLException ex){
                ex.printStackTrace();
            }
            e.printStackTrace();
        }


        return isRight;
    }

    @Override
    public boolean deleteBean(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao =new GuestbookDaoImpl();
        //DBManager中获取到的连接传递给BaseDao中的setConnection(conn)
        guestbookDao.setConnection(connection);
        boolean isRight = false;
        try {
            isRight =  guestbookDao.deleteByid(id);
            connection.commit();
        } catch (SQLException e){
            try {
            connection.rollback();
        } catch (SQLException ex){
            ex.printStackTrace();
        }
            e.printStackTrace();
        }


        return isRight;
    }

    @Override
    public List<Object> queryInfo() {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(connection);
        List<Object> list = null;
        try {
            list = guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        Pagination pagination = null;
        try {
            pagination = guestbookDao.getGuestbookByPage(pageSize,pageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pagination;
    }

    @Override
    public Guestbook findAll(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection connection =dbManager.getConnection();
        GuestbookDaoImpl dao = new GuestbookDaoImpl();
        dao.setConnection(connection);
        Guestbook guestbook= null;
        try {
            guestbook =dao.findAllByid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return guestbook;
    }

    @Override
    public boolean updateBean(Guestbook guestbook) {
        DBManager dbManager= DBManager.getInstance();
        Connection connection =dbManager.getConnection();
        GuestbookDaoImpl dao  = new GuestbookDaoImpl();
        dao.setConnection(connection);
        boolean isOK=false;
        try {
             isOK = dao.updateByid(guestbook);
             connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isOK;
    }
}






































